{extend name="common/index"}
{block name="css"}
<style>
</style>
{/block}

{block name="content"}
<div class="layui-row">
    <form id="editForm" class="layui-form" action="">
        <input type="hidden" name="id" value="{$data.id|default=''}">
        <div class="layui-field-box">
            <div class="layui-main">
                <div class="layui-form-item">
                    <label class="layui-form-label">省份：</label>
                    <div class="layui-input-block">
                        <select name="province" id="province" lay-filter="region">
                            <option value="">请选择</option>
                            {volist name="provinceList" id="vo"}
                            <option value="{$vo.id}" {eq name="vo.id" value="$data.province|default=''"}selected{/eq}>{$vo.area_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">城市：</label>
                    <div class="layui-input-block">
                        <select name="city" id="city" lay-filter="region">
                            <option value="">请选择</option>
                            {notempty name="cityList"}
                            {volist name="cityList" id="vo"}
                            <option value="{$vo.id}" {eq name="vo.id" value="$data.city|default=''"}selected{/eq}>{$vo.area_name}</option>
                            {/volist}
                            {/notempty}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详细地址：</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" value="{$data.address|default=''}" class="layui-input">
                    </div>
                </div>
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">联系人：</label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="text" name="contact" value="{$data.contact|default=''}" class="layui-input">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">电话：</label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="text" name="tel" value="{$data.tel|default=''}" class="layui-input">-->
                    <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <label class="layui-form-label">排序：</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort_num" value="{$data.sort_num|default='1'}" placeholder="数字越小，越靠前" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="tpl-form-action">
                <button class="layui-btn layui-btn-radius layui-btn-lg" lay-submit lay-filter="formSubmit">保存</button>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="script"}

<script type="text/javascript">
    layui.use(['form'], function () {
        var form = layui.form;

        //监听省市区三级联动
        form.on('select(region)', function (data) {
            var id = data.value;
            var position_now = $(data.elem).attr('id');
            var position_next = '';
            var position_change = '';

            if (position_now == 'province') {
                position_next = '#city';
            }

            ajaxRegion(id, position_next, position_change);
        });

        /**
         * 省市区变更
         */
        function ajaxRegion(id, position, position_change) {
            $.post('{:url("getRegion")}', {pid:id}, function (result) {
                var _html = "<option value=''>请选择</option>";
                $.each(result, function (key, val) {
                    _html += "<option value='"+val.id+"'>"+val.area_name+"</option>";
                });
                $(position).html(_html);
                $(position_change).html("<option value=''>请选择</option>");
                form.render('select');
            });
        }

        //监听提交
        form.on('submit(formSubmit)', function(data){
            var index = layer.load(2, {shade:[0.5,'#000'],time: 10*1000});
            $.post('{:url("domainSave")}', $('#editForm').serialize(), function (result) {
                layer.close(index);
                if (result.code) {
                    layer.msg(result.msg, {shade:[0.5,'#000'],time:1000}, function () {
                        window.location.href = result.url;
                    });
                } else {
                    layer.alert(result.msg, {icon:2, title:'保存失败！'});
                }
            }, 'json');
            return false;
        });
    });

</script>
{/block}